﻿// (c) Copyright Fernando Cerqueira.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.VisualStudio.PlatformUI;
using AppRes = WindowsPhoneCommands.Resources;

// ReSharper disable CheckNamespace
namespace WindowsPhoneCommands
// ReSharper restore CheckNamespace
{
    /// <summary>
    /// Interaction logic for XapDeploy.xaml
    /// </summary>
    // ReSharper disable RedundantExtendsListEntry
    public partial class NewKeyResourceDialog : DialogWindow
    // ReSharper restore RedundantExtendsListEntry
    {
        public string Key;
        public string Value;
        public string Comment;
        public ItemCollection Items;
        private bool _loaded;

        public NewKeyResourceDialog()
        {
            InitializeComponent();
            Background = VsTheming.GetDialogBackground();
            Foreground = VsTheming.GetWindowText();
            Loaded += NewKeyResourceDialogLoaded;
        }

        private void NewKeyResourceDialogLoaded(object sender, RoutedEventArgs e)
        {
            if (!_loaded)
            {
                Title = AppRes.ToolWindowTitle + " " + AppRes.XTitleNewKey;
                TxtKey.Focus();
            }
            _loaded = true;
        }

        private void BntOkClick(object sender, RoutedEventArgs e)
        {
            if (ValidateFields(true))
            {
                var existskey = false;
                // ReSharper disable LoopCanBeConvertedToQuery
                foreach (var itemdg in Items)
                // ReSharper restore LoopCanBeConvertedToQuery
                {
                    var it = itemdg as ModelResxTranslateAll;
                    if (it != null)
                    {
                        if (it.Key.ToLower() == TxtKey.Text.Trim().ToLower())
                        {
                            existskey = true;
                            break;
                        }
                    }
                }
                if (existskey)
                {
                    MessageBox.Show(AppRes.ErrKeyExists, AppRes.ToolWindowTitle, MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    Key = TxtKey.Text.Trim();
                    Value = TxtValue.Text;
                    Comment = TxtComment.Text.Trim();
                    Close();
                }
            }
        }

        private static int Adorne(Control control, bool error, string tootip)
        {
            if (error)
            {
                var colorerr = VsTheming.GetColorError();
                if (control.BorderBrush != colorerr)
                {
                    control.Tag = control.BorderBrush;
                    control.BorderBrush = colorerr;
                    control.ToolTip = tootip;
                }
            }
            else
            {
                if (control.BorderBrush != control.Tag && control.Tag != null)
                {
                    control.BorderBrush = (Brush)control.Tag;
                }
            }
            return error ? 1 : 0;
        }

        private bool ValidateFields(bool showmsg)
        {
            var qtd = Adorne(TxtKey, string.IsNullOrEmpty(TxtKey.Text.Trim()), AppRes.RequiredField);
            qtd += Adorne(TxtValue, string.IsNullOrEmpty(TxtValue.Text.Trim()), AppRes.RequiredField);
            if (qtd > 0 && showmsg)
            {
                MessageBox.Show(AppRes.ErrCompleteFields, AppRes.ToolWindowTitle, MessageBoxButton.OK,MessageBoxImage.Error);
            }
            return (qtd == 0);
        }

        private void TxtKeyLostFocus(object sender, RoutedEventArgs e)
        {
            ValidateFields(false);
        }

        private void TxtValueLostFocus(object sender, RoutedEventArgs e)
        {
            ValidateFields(false);
        }


    }
}
